const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
const screenservice = require("../../../apiservice/class/classservice");
const leadservice = require("../../../apiservice/lead/leadservice");

Page({
	data: {
		schoolScreen: null,
		classData: [],
		selectclassIndex: -1,
		selectclassName: "",
		groupStudent: [],
		height: wx.getSystemInfoSync().windowHeight-120,
		stage:''
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onLoad: function(option) {

		var res = wx.getSystemInfoSync();

		this.setData({
			schoolScreen: option
		})
	},

	onShow: async function() {
		var screen = this.data.schoolScreen;
		var result = await screenservice.QueryClassBySchoolIdAndClassName(screen.schoolId, "");
		if (result.data) {
			//debugger;
			this.setData({
				classData: result.data
			})
		}
		console.log(this.data.schoolScreen);

	},

	classChange: async function(e) {
		this.setData({
			selectclassIndex: e.detail.value,
			selectclassName: this.data.classData[e.detail.value].new_classnumber
		});

		var classObj = this.data.classData[e.detail.value];
		var result = await leadservice.GetLeadInfoBySchoolIdAndClassNum(classObj.new_classId);
		this.setData({
			groupStudent: result.data

		})
		console.log(result.data);
	},

	onIndexChange: function(e) {
		console.log(e);
	},

	onEnterDetail: function(e) {
		var that = this;
		var schoolScreen = this.data.schoolScreen;
		var currentTarget = e.currentTarget;
		var sccustid = e.currentTarget.id;
		var leadid = e.currentTarget.id;
		let leadItem = currentTarget.dataset.item;

		let leadObj = {
			leadid: leadItem.leadId,
			age: leadItem.new_age ? leadItem.new_age : 0,
			customername: leadItem.fullName,
			createdon: leadItem.createdOn,
			iphoneno1: leadItem.address1_Telephone1 ? leadItem.address1_Telephone1 : "",
		};
		//debugger;
		var toUrl = "../../screens/result/type/type?" +
			`scid=${schoolScreen.scid}&leadname=${currentTarget.dataset.name}` +
			`&sccustid=${sccustid}&leadid=${leadid}&schoolId=${schoolScreen.schoolId}`+
			`&stage=${schoolScreen.stage}`
		wx.navigateTo({
			url: toUrl,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: leadObj
				})
			}
		})

		console.log(e);
	},

	onNewStudent: function(e) {
		var that = this;
		var selectclassIndex = this.data.selectclassIndex;
		var classObj = this.data.classData[selectclassIndex];
		var schoolScreen = this.data.schoolScreen;
		var url = "";
		if (this.data.selectclassIndex == -1) {
			url = "../addstudent/addstudent?" +
				`new_screenId=${schoolScreen.scid}&new_screenteamId=${schoolScreen.screenteamId}` +
				`&templateid=${schoolScreen.templateid}&schoolId=${schoolScreen.schoolId}` +
				`&schoolName=${schoolScreen.schoolName}&stage=${schoolScreen.stage}&classId=&className=&&classNum=` +
				`&ownerid=${schoolScreen.ownerid}`;
		} else {
			url = "../addstudent/addstudent?" +
				`new_screenId=${schoolScreen.scid}&new_screenteamId=${schoolScreen.screenteamId}` +
				`&templateid=${schoolScreen.templateid}&schoolId=${schoolScreen.schoolId}` +
				`&schoolName=${schoolScreen.schoolName}&stage=${schoolScreen.stage}`+
				`&classId=${classObj.new_classId}&className=${classObj.new_name}&classNum=${classObj.new_classnumber}` +
				`&ownerid=${schoolScreen.ownerid}`;
		}

		wx.navigateTo({
			url: url,
			events: {
				refreshEvent: async data => {
					if (classObj) {
						//debugger;
						var result = await leadservice.GetLeadInfoBySchoolIdAndClassNum(classObj.new_classId);
						that.setData({
							groupStudent: result.data
						})
					}
				}
			}
		})
		console.log(e);
	},

})
